<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- <img src="../assets/heima.png" alt="" /> -->
        <img src="../assets/xiaoche.png" alt="" />
        <span>物流管理系统</span>
      </div>
      <div>
        <p>
          你好，<a href="javascript:;">{{
            userInfo.nickname || userInfo.username
          }}</a
          >,欢迎进入系统&nbsp;&nbsp;&nbsp;
        </p>
        <!-- <el-button type="info" @click="logout">管理</el-button> -->
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 菜单折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333945"
          text-color="#fff"
          active-text-color="#4473c4"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单--*图标导入失败* -->
          <!-- index只接收 字符串 ，用: 绑定后，传进去的是数值 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/home/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/home/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'MyHome',
  data() {
    return {
      menulist: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      // menuData: [],
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    console.log(this.userInfo, 'aaaaaaaaaa')
  },
  // mounted() {
  //   this.initNavigation()
  // },
  computed: {
    userInfo() {
      return this.$store.state.userInfo
    },
  },
  methods: {
    // 退出功能
    logout() {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    // 获取所有的菜单数据
    async getMenuList() {
      // 发起 axios 请求菜单数据
      const { data: res } = await this.$http.get('/package/data/menu.json')
      // 状态码 不等于200 请求失败
      // if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 成功后 将得到的数据赋值给 menulist
      this.menulist = res
      console.log(res, '======================')
      console.log(this.menulist)
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: #31373a;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 61px;
      height: 65px;
      background-size: contain;
      background-color: #31373a;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333945;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #e7ecef;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #485264;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>
